package pl.register.android.activities.program.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import pl.register.android.R;
import pl.register.android.activities.program.logic.ProgramPresenter;
import pl.register.android.homework.HomeworkTopic;
import pl.register.android.classes.ClassesTopic;
import pl.register.android.chapter.ChapterTitle;
import pl.register.android.lesson.LessonProgram;
import pl.register.android.util.StringUtils;
import pl.register.android.widget.listview.RefreshableAdapter;

import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-09
 */
public class LessonProgramListAdapter extends RefreshableAdapter<LessonProgram>
        implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private ProgramPresenter programPresenter;

    private ProgramView programView;

    public LessonProgramListAdapter(Context context, ProgramPresenter programPresenter, ProgramView programView) {
        super(context);
        this.programPresenter = programPresenter;
        this.programView = programView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_program_row, null);
            holder.llRow = (LinearLayout) convertView.findViewById(R.id.lesson_program_row);
            holder.btnLessonProgram = (Button) convertView.findViewById(R.id.lesson_program_btn);
            holder.llSecondLesson = (LinearLayout) convertView.findViewById(R.id.second_lesson_linear_layout);
            holder.tvOrdinalNumber = (TextView) convertView.findViewById(R.id.ordinal_number_text_view);
            holder.cbLessonTopicDone = (CheckBox) convertView.findViewById(R.id.lesson_done_check_box);
            holder.cbLessonTopicDone.setFocusable(false);
            holder.tvLessonTopic = (TextView) convertView.findViewById(R.id.lesson_topic_button);
            holder.cbSecondLessonTopicDone = (CheckBox) convertView.findViewById(R.id.second_lesson_done_check_box);
            holder.cbSecondLessonTopicDone.setFocusable(false);
            holder.tvSecondLessonTopic = (TextView) convertView.findViewById(R.id.second_lesson_topic_button);
            holder.cbHomeworkTopicDone = (CheckBox) convertView.findViewById(R.id.homework_done_check_box);
            holder.cbHomeworkTopicDone.setFocusable(false);
            holder.tvHomeworkTopic = (TextView) convertView.findViewById(R.id.homework_topic_button);
            holder.tvChapterLesson = (TextView) convertView.findViewById(R.id.chapter_title_button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        fillRow(holder, position);
        setListeners(holder);
        return convertView;
    }

    private void fillRow(ViewHolder holder, int position) {
        LessonProgram program = items.get(position);
        holder.btnLessonProgram.setTag(program);
        holder.tvOrdinalNumber.setText(StringUtils.build(position + 1));

        List<ClassesTopic> classesTopics = program.getClassesTopics();
        ClassesTopic firstClassesTopic = classesTopics.get(0);
        holder.tvLessonTopic.setText(firstClassesTopic.getTopic());
        holder.cbLessonTopicDone.setTag(firstClassesTopic);
        holder.cbLessonTopicDone.setChecked(firstClassesTopic.isDone());
        if (classesTopics.size() == 2) {
            ClassesTopic secondClassesTopic = classesTopics.get(1);
            holder.tvSecondLessonTopic.setText(secondClassesTopic.getTopic());
            holder.cbSecondLessonTopicDone.setTag(secondClassesTopic);
            holder.cbSecondLessonTopicDone.setChecked(secondClassesTopic.isDone());
            holder.llRow.getLayoutParams().height = context.getResources().getDimensionPixelSize(R.dimen.height_double_list_row);
            holder.btnLessonProgram.getLayoutParams().height = context.getResources().getDimensionPixelSize(R.dimen.height_double_list_row);
            holder.llSecondLesson.setVisibility(View.VISIBLE);

        } else {
            holder.llRow.getLayoutParams().height = context.getResources().getDimensionPixelSize(R.dimen.height_list_row);
            holder.btnLessonProgram.getLayoutParams().height = context.getResources().getDimensionPixelSize(R.dimen.height_list_row);
            holder.llSecondLesson.setVisibility(View.GONE);
        }

        HomeworkTopic homeworkTopic = program.getHomeworkTopic();
        holder.tvHomeworkTopic.setText(homeworkTopic.getTopic());
        holder.cbHomeworkTopicDone.setTag(homeworkTopic);
        holder.cbHomeworkTopicDone.setChecked(homeworkTopic.isDone());

        ChapterTitle chapterTitle = program.getChapterTitle();
        holder.tvChapterLesson.setText(chapterTitle.getTitle());
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        switch (compoundButton.getId()) {
            case R.id.lesson_done_check_box:
            case R.id.second_lesson_done_check_box:
                ClassesTopic classesTopic = (ClassesTopic) compoundButton.getTag();
                classesTopic.setDone(isChecked);
                programPresenter.saveClassesTopic(classesTopic);
                break;
            case R.id.homework_done_check_box:
                HomeworkTopic homeworkTopic = (HomeworkTopic) compoundButton.getTag();
                homeworkTopic.setDone(isChecked);
                programPresenter.saveHomeworkTopic(homeworkTopic);
                break;
        }
    }

    private void setListeners(ViewHolder holder) {
        holder.cbLessonTopicDone.setOnCheckedChangeListener(this);
        holder.cbSecondLessonTopicDone.setOnCheckedChangeListener(this);
        holder.cbHomeworkTopicDone.setOnCheckedChangeListener(this);
        holder.btnLessonProgram.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lesson_program_btn:
                LessonProgram lessonProgram = (LessonProgram) v.getTag();
                 programView.showEditProgramDialog(lessonProgram);
                break;
        }
    }

    public static class ViewHolder {
        public LinearLayout llRow;
        public Button btnLessonProgram;
        public TextView tvOrdinalNumber;
        public TextView tvLessonTopic;
        public TextView tvHomeworkTopic;
        public TextView tvChapterLesson;
        public CheckBox cbLessonTopicDone;
        public CheckBox cbHomeworkTopicDone;

        public LinearLayout llSecondLesson;
        public CheckBox cbSecondLessonTopicDone;
        public TextView tvSecondLessonTopic;
    }
}